<template>
  <avue-crud :option="option"
             :table-loading="loading"
             :data="data"
             v-model:page="page"
             v-model="form"
             ref="crud"
             @search-change="searchChange"
             @search-reset="searchReset"
             @selection-change="selectionChange"
             @current-change="currentChange"
             @size-change="sizeChange"
             @on-load="onLoad">
    <template #menu-left>
      <el-button type="primary"
                 icon="el-icon-pointer"
                 @click="viewSub">前往编辑订阅
      </el-button>
    </template>
  </avue-crud>
</template>

<script>
import {getList} from "@/api/iot/server/serverRelay";
import { validatenull } from '@/utils/validate';

export default {
  name: 'productSub',
  props: {
    productKey: String,
    productId: String,
  },
  data() {
    return {
      form: {},
      query: {},
      loading: true,
      page: {
        pageSize: 10,
        currentPage: 1,
        total: 0
      },
      selectionList: [],
      option: {
        height: 'auto',
        calcHeight: 32,
        searchShow: true,
        searchMenuSpan: 6,
        tip: false,
        border: true,
        index: true,
        addBtn: false,
        viewBtn: true,
        selection: true,
        menu: false,
        dialogWidth: 500,
        labelWidth: 120,
        searchLabelWidth: 100,
        column: [
          {
            label: "所属产品",
            prop: "productKey",
            type: "select",
            dicUrl: "/blade-iot/product/select",
            props: {
              label: "productName",
              value: "productKey"
            },
            width: 250,
            span: 24,
          },
          {
            label: "消费组",
            prop: "groupId",
            type: "select",
            dicUrl: "/blade-iot/server/group/select",
            dataType: "string",
            props: {
              label: "groupName",
              value: "groupId"
            },
            multiple: true,
            span: 24,
          },
          {
            label: "功能类型",
            prop: "features",
            type: 'select',
            dicUrl: "/blade-system/dict/dictionary?code=topic_feature",
            dataType: "string",
            props: {
              label: "dictValue",
              value: "dictKey"
            },
            multiple: true,
            span: 24,
          },
        ]
      },
      data: []
    };
  },
  computed: {
    ids() {
      let ids = [];
      this.selectionList.forEach(ele => {
        ids.push(ele.id);
      });
      return ids.join(",");
    }
  },
  watch: {
    productId() {
      if (!validatenull(this.productId)) {
        this.onLoad(this.page);
      }
    }
  },
  methods: {
    viewSub() {
      window.open(`/iot/server/relay?productKey=${this.productKey}`);
    },
    searchReset() {
      this.query = {};
      this.onLoad(this.page);
    },
    searchChange(params, done) {
      this.query = params;
      this.page.currentPage = 1;
      this.onLoad(this.page, params);
      done();
    },
    selectionChange(list) {
      this.selectionList = list;
    },
    selectionClear() {
      this.selectionList = [];
      this.$refs.crud.toggleSelection();
    },
    currentChange(currentPage) {
      this.page.currentPage = currentPage;
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
    },
    onLoad(page, params = {}) {
      this.loading = true;
      getList(page.currentPage, page.pageSize, Object.assign(params, {productKey: this.productKey})).then(res => {
        const data = res.data.data;
        this.page.total = data.total;
        this.data = data.records;
        this.loading = false;
        this.selectionClear();
      });
    }
  }
};
</script>

<style>
</style>
